package com.example.mainactivity;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.LinkedList;

import org.apache.http.util.EncodingUtils;

import android.content.Context;
import android.content.res.AssetManager;
import android.widget.Toast;

public class MyFile {
	private Context mContext;
	private String ASSETS_PATH = "file";
	private String BACKSLASH = "/"; 
	private IMyFileListener mIMyFile;
	private LinkedList<WaitTask>  mWaitTask;
	
	public interface IMyFileListener{
		public void setData(LinkedList<WaitTask> wait_task);
	}
	
	public void setIMyFileListener(IMyFileListener myfile){
		mIMyFile = myfile;
	}
	
	
	
	public MyFile(Context context){
		mContext = context;
		mWaitTask = new LinkedList<WaitTask>();
	}
	
	public void getData(){
		setAssetFile();
	}
	
	public void setAssetFile(){
		try {
			AssetManager am = mContext.getAssets();
			String[] files = am.list(ASSETS_PATH);
			for (String file : files) {
				InputStream input = mContext.getAssets().open(
						ASSETS_PATH + BACKSLASH + file);
				byte[] b = new byte[5];
				int len;
				while ((len = input.read(b)) != -1) {
					mWaitTask.addLast(new WaitTask(EncodingUtils.getString(b, "UTF-8")));
				}
				input.close();
			}
			mIMyFile.setData(mWaitTask);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public class WaitTask implements Runnable{  
	    public String data;  
	    public WaitTask(String data){  
	        this.data=data;  
	    }  
	    @Override  
	    public void run() {  
	        try {  
	            Thread.sleep(1500);  
	        } catch (InterruptedException e) {  
	            e.printStackTrace();  
	        }  
	        System.out.println(data);
	    }  
	    public String getFileId(){  
	        return data;  
	    }  
	  
	}  
}
